package com.mach.platform.api.controller;

import com.mach.platform.api.domain.WxUser;
import com.mach.platform.api.service.AttentServiceImpl;
import com.mach.platform.api.service.TeamServiceImpl;
import com.mach.platform.api.service.WxUserServiceImpl;
import com.mach.platform.domain.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = {"我的页面接口"})
@RestController
@RequestMapping("/api")
public class MineCtrl {

    @Autowired
    private AttentServiceImpl attentService;

    @Autowired
    private TeamServiceImpl teamService;

    @Autowired
    private WxUserServiceImpl userService;

    /**
     * 获取用户信息
     * @param request
     * @return
     */
    @ApiOperation(value = "获取用户信息")
    @PostMapping("/getWxUserInfo")
    public Response getWxUserInfo(HttpServletRequest request) {
        String openId = request.getHeader("openId");
        WxUser user = userService.getWxUserInfo(openId);
        return Response.ok(user);
    }


    /**
     * 1. 我的关注相关接口-----
     */

    /**
     * 我的-我的关注：设备组关注列表
     * @param request
     * @param orderStatus 排序字段  faultNum：故障排序 runNum：运行排序 offlineNum：离线排序 standbyNum：待机排序
     * @return
     */
    @ApiOperation(value = "我的-我的关注：设备组关注列表")
    @PostMapping("/attention/attentDevGroups")
    public Response attentDevGroups(HttpServletRequest request, String orderStatus) throws SQLException {
        String openId = request.getHeader("openId");
        Map result = attentService.attentDevGroupList(openId, orderStatus);
        return Response.ok(result);
    }


    /**
     * 取消关注设备组/设备
     * @param ids 关注id的数组
     * @return
     */
    @ApiOperation(value = "取消关注设备组/设备")
    @PostMapping("/attention/cancelAttent")
    public Response cancelAttent(Long[] ids) {
        boolean flag = attentService.cancelAttent(ids);
        if (flag) {
            return Response.ok("取消关注成功");
        } else {
            return Response.error("取消关注失败，请重新尝试");
        }
    }


    /**
     * 取消置顶设备组/设备
     * @param id 关注id
     * @return
     */
    @ApiOperation(value = "取消置顶设备组/设备")
    @PostMapping("/attention/cancelTopping")
    public Response cancelTopping(Long id) {
        int count = attentService.cancelTopping(id);
        if (count != 1) {
            return Response.error("取消置顶失败");
        }
        return Response.ok("取消置顶成功");
    }


    /**
     * 置顶设备组/设备
     * @param id 关注的id
     * @return
     */
    @ApiOperation(value = "置顶设备组/设备")
    @PostMapping("/attention/topping")
    public Response topping(Long id) {
        int count = attentService.topping(id);
        if(count != 1) {
            Response.error("置顶失败");
        }
        return Response.ok("置顶成功");
    }


    /**
     * 关注设备分类列表
     * @param request
     * @return
     */
    @ApiOperation(value = "关注设备分类集合")
    @PostMapping("/attention/attentDevSortList")
    public Response attentDevSortList(HttpServletRequest request) {
        String openId = request.getHeader("openId");
        List result = attentService.attentDevSortList(openId);
        return Response.ok(result);
    }


    /**
     * 我的关注：设备关注列表
     * @param request
     * @param sortName 分类名称
     * @return
     */
    @ApiOperation(value = "我的关注：设备关注列表")
    @PostMapping("/attention/attentDevices")
    public Response attentDevices(HttpServletRequest request, String sortName) {
        String openId = request.getHeader("openId");
        Map result = attentService.attentDeviceList(openId, sortName);
        return Response.ok(result);
    }


    /**
     * 2. 我的客户服务相关接口-----
     */



    /**
     * 3. 我的团队相关的接口------
     */


    /**
     * 获取用户所有团队
     * @return
     */
    @ApiOperation(value = "获取用户所有团队")
    @PostMapping("/team/findTeamList")
    public Response findTeamList(HttpServletRequest request) {
        String openId = request.getHeader("openId");
        List<Map> result = teamService.findTeamList(openId);
        return Response.ok(result);
    }

    /**
     * 获取团队成员信息
     * @param request
     * @param stationId 站房id
     * @return
     */
    @ApiOperation(value = "获取团队成员信息")
    @PostMapping("/team/findTeamMemberInfo")
    public Response findTeamMemberInfo(HttpServletRequest request, Long stationId) {
        String openId = request.getHeader("openId");
        Map result = teamService.findTeamMemberInfo(openId, stationId);
        return Response.ok(result);
    }


    /**
     * 删除团队成员
     * @param stationId 团队/站房id
     * @param memberIds 删除的成员openIds
     * @return
     */
    @ApiOperation(value = "删除团队成员")
    @PostMapping("/team/deleteMember")
    public Response deleteMember(Long stationId, String[] memberIds) {
        teamService.delete(stationId, memberIds);
        return Response.ok("删除团员成功");
    }


    /**
     * 加入某个团队
     * @param request
     * @param stationId 团队站房id
     * @return
     */
    @ApiOperation(value = "加入某个团队")
    @PostMapping("/team/joinTeam")
    public Response joinTeam(HttpServletRequest request, Long stationId) {
        String openId = request.getHeader("openId");
        boolean isMember = teamService.checkIsMember(openId, stationId);
        if (isMember) {
            return Response.error("您已经加入该团队，请勿重新操作");
        }
        teamService.joinTeam(openId, stationId);
        return Response.ok("成功加入团队");
    }

    /**
     * 转让团队领队
     * @param stationId 团队站房id
     * @param newLeader 新领队openId
     * @return
     */
    @ApiOperation(value = "转让团队领队")
    @PostMapping("/team/transferLeader")
    public Response transferLeader(HttpServletRequest request, Long stationId, String newLeader) {
        String openId = request.getHeader("openId");
        boolean result = teamService.transferLeader(stationId, openId, newLeader);
        if (!result) {
            return Response.error("转让领队失败");
        }
        return Response.ok("转让领队成功");
    }


    /**
     * 4. 我的能源账单相关接口-----
     */


    /**
     * 5. 设置的相关接口-----
     */


    /**
     * 修改用户头像
     * @param file 修改后的头像图片
     * @return
     */
    @ApiOperation(value = "修改头像")
    @PostMapping("/setting/updateAvatar")
    public Response updateAvatar(HttpServletRequest request, MultipartFile file) {
        String openId = request.getHeader("openId");
        String avatarPath = userService.updateAvatar(openId, file);
        Map result = new HashMap();
        result.put("avatarPath", avatarPath);
        return Response.ok(result);
    }


    /**
     * 修改用户昵称
     * @param request
     * @param nickName 修改后的用户昵称
     * @return
     */
    @ApiOperation(value = "修改用户昵称")
    @PostMapping("/setting/updateNickname")
    public Response updateNickname(HttpServletRequest request, String nickName) {
        String openId = request.getHeader("openId");
        userService.updateNickname(openId, nickName);
        return Response.ok("修改昵称成功");
    }


    /**
     * 用户退出登录
     * @param request
     * @return
     */
    @ApiOperation(value = "用户退出登录")
    @PostMapping("/setting/signOut")
    public Response signOut(HttpServletRequest request) {
        String openId = request.getHeader("openId");
        userService.signOut(openId);
        return Response.ok("退出登录成功");
    }


}
